package com.decorator.rocketmq.service;

import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;


/*
 * links:https://blog.csdn.net/qq_26154077/article/details/111013842
 * topic = "${rocketmq.consumer.topic}"
 * selectorExpression = "${rocketmq.consumer.tags}" 监听的tags
 *
 * */

@Service
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "asyncSendTopic")
public class RocketMQConsumer implements RocketMQListener<String> {

    private int total =0;
    private int delayTotal=0;
    @Override
    public void onMessage(String o) {
        total+=1;
        try {
            if (o.hashCode() % 3 == 0) {
                Thread.sleep(Math.round(5000));
                System.out.println("延迟的消息"+o);
                delayTotal+=1;
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(o);
        System.out.println("total: "+total+"; delayTotal:"+delayTotal);

    }
}
